const weapi = require('./index.js')

// 账号密码登录
const tryTmplogin = (username, password) => {
  console.log('尝试更新TOKEN  账号密码登录')
  return new Promise((resolve, reject) => {
    weapi.tmplogin(username, password).then(res => {
      console.log('res', res)
      var token = res.data
      wx.setStorageSync('token', token)
      wx.setStorageSync('tokenmode', 'username_and_password')
      wx.setStorageSync('tokentime', Date.now())
      resolve()
    }, () => {
      wx.clearStorage()
      reject()
    })
  })
}

// 微信快速登录
const tryWeLogin = () => {
  console.log('尝试更新TOKEN  微信快速登录')
  return new Promise((resolve, reject) => {
    wx.login({
      success (res) {
        if (res.code) {
          // 微信登录
          weapi.welogin(res.code).then(res => {
            var token = res.data
            wx.setStorageSync('token', token)
            wx.setStorageSync('tokenmode', 'wechat')
            wx.setStorageSync('tokentime', Date.now())
            resolve()
          }, () => {
            wx.clearStorage()
            reject()
          })
        } else {
          wx.clearStorage()
          reject()
        }
      }
    })
  })
}

// 尝试更新TOKEN
const tryUpdateToken = () => {
  return new Promise((resolve, reject) => {
    var token = wx.getStorageSync('token')
    if (!token) {
      wx.clearStorage()
      reject && reject()
      return
    }
    var mode  = wx.getStorageSync('tokenmode')
    var time  = wx.getStorageSync('tokentime')
    var d1 = 1000 * 60 * 60 * 24
    if (Date.now() > (time + d1) || Date.now() > (time + (d1 / 2))) {
      // 已过期
      // 未过期，进行token更新
      if (mode == 'username_and_password') {
        // 账号密码登录
        var user = wx.getStorageSync('login_username')
        var pass = wx.getStorageSync('login_password')
        tryTmplogin(user, pass).then(() => {
          resolve()
        }, () => {
          reject()
        })
      } else {
        // 微信快捷登录
        tryWeLogin().then(() => {
          resolve()
        }, () => {
          reject()
        })
      }
    } else {
      // 正在有效期内
      resolve()
    }
  })
}

// 发送订阅许可
const requestTemplateMessage = () => {
  // 尝试询问用户订阅消息
  weapi.templatelist().then(res => {
    var tmplIds = res.data.map(item => {
      return item.value
    })
    if (tmplIds.length > 0) {
      wx.requestSubscribeMessage({
        tmplIds: tmplIds,
        success (res) {
          console.log('订阅结果 success', res)
        },
        fail (res) {
          console.log('订阅结果 fail', res)
        },
      })
    }
  })
}

// 是否为店铺用户
const getUserType = () => {
  var user = wx.getStorageSync('user')
  return user.roles[0]
}

// 当前用户名
const getUserName = () => {
  var user = wx.getStorageSync('user')
  return user.userName
}
const getRealName = () => {
  var user = wx.getStorageSync('user')
  return user.realName
}

// 当前用户所属单位类型
const getOrgType = () => {
  var user = wx.getStorageSync('user')
  return user.organizationType
}

// 是否为执法用户
const iszfuser = () => {
  var user = wx.getStorageSync('user')
  return user.roles.indexOf('zfuser') > -1
}

// 是否为审批用户
const isreviewer = () => {
  var user = wx.getStorageSync('user')
  return user.roles.indexOf('reviewer') > -1
}

// 是否为店铺用户
const isdpuser = () => {
  var user = wx.getStorageSync('user')
  return user.roles.indexOf('dpuser') > -1
}

// 是否微信绑定
const iswebind = () => {
  var user = wx.getStorageSync('user')
  return user.openid != null && user.openid.length > 0
}

module.exports = {
  tryUpdateToken,
  requestTemplateMessage,
  getUserType,
  getUserName,
  getRealName,
  getOrgType,
  iszfuser,
  isreviewer,
  isdpuser,
  iswebind
}
